package com.hotel.dao;

import com.hotel.model.Booking;
import com.hotel.util.DBUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;


public class BookingDAO {
    
    public boolean addBooking(Booking booking) {
        String sql = "INSERT INTO bookings (room_id, customer_name, customer_phone, customer_email, " +
                    "check_in_date, check_out_date, total_amount, status, booking_date) " +
                    "VALUES (?, ?, ?, ?, ?, ?, ?, ?, NOW())";
        
        Connection conn = null;
        try {
            conn = DBUtil.getConnection();
            PreparedStatement stmt = conn.prepareStatement(sql);
            
            stmt.setInt(1, booking.getRoomId());
            stmt.setString(2, booking.getCustomerName());
            stmt.setString(3, booking.getCustomerPhone());
            stmt.setString(4, booking.getCustomerEmail());
            stmt.setDate(5, booking.getCheckInDate());
            stmt.setDate(6, booking.getCheckOutDate());
            stmt.setBigDecimal(7, booking.getTotalAmount());
            stmt.setString(8, booking.getStatus());
            
            int result = stmt.executeUpdate();
            stmt.close();
            
            System.out.println("添加预订结果: " + result);
            return result > 0;
            
        } catch (SQLException e) {
            System.err.println("添加预订失败: " + e.getMessage());
            e.printStackTrace();
            return false;
        } finally {
            DBUtil.closeConnection(conn);
        }
    }
    
    public List<Booking> getAllBookings() {
        List<Booking> bookings = new ArrayList<>();
        String sql = "SELECT * FROM bookings ORDER BY booking_date DESC";
        
        Connection conn = null;
        try {
            conn = DBUtil.getConnection();
            PreparedStatement stmt = conn.prepareStatement(sql);
            ResultSet rs = stmt.executeQuery();
            
            while (rs.next()) {
                Booking booking = new Booking();
                booking.setBookingId(rs.getInt("booking_id"));
                booking.setRoomId(rs.getInt("room_id"));
                booking.setCustomerName(rs.getString("customer_name"));
                booking.setCustomerPhone(rs.getString("customer_phone"));
                booking.setCustomerEmail(rs.getString("customer_email"));
                booking.setCheckInDate(rs.getDate("check_in_date"));
                booking.setCheckOutDate(rs.getDate("check_out_date"));
                booking.setTotalAmount(rs.getBigDecimal("total_amount"));
                booking.setStatus(rs.getString("status"));
                booking.setBookingDate(rs.getDate("booking_date"));
                bookings.add(booking);
            }
            
            rs.close();
            stmt.close();
            
        } catch (SQLException e) {
            System.err.println("获取预订列表失败: " + e.getMessage());
            e.printStackTrace();
        } finally {
            DBUtil.closeConnection(conn);
        }
        
        return bookings;
    }
    
    public boolean updateBookingStatus(int bookingId, String status) {
        String sql = "UPDATE bookings SET status = ? WHERE booking_id = ?";
        
        Connection conn = null;
        try {
            conn = DBUtil.getConnection();
            PreparedStatement stmt = conn.prepareStatement(sql);
            
            stmt.setString(1, status);
            stmt.setInt(2, bookingId);
            
            int result = stmt.executeUpdate();
            stmt.close();
            
            System.out.println("更新预订状态结果: " + result + ", bookingId: " + bookingId + ", status: " + status);
            return result > 0;
            
        } catch (SQLException e) {
            System.err.println("更新预订状态失败: " + e.getMessage());
            e.printStackTrace();
            return false;
        } finally {
            DBUtil.closeConnection(conn);
        }
    }
    
    public Booking getBookingById(int bookingId) {
        String sql = "SELECT * FROM bookings WHERE booking_id = ?";
        Connection conn = null;
        
        try {
            conn = DBUtil.getConnection();
            PreparedStatement stmt = conn.prepareStatement(sql);
            stmt.setInt(1, bookingId);
            ResultSet rs = stmt.executeQuery();
            
            if (rs.next()) {
                Booking booking = new Booking();
                booking.setBookingId(rs.getInt("booking_id"));
                booking.setRoomId(rs.getInt("room_id"));
                booking.setCustomerName(rs.getString("customer_name"));
                booking.setCustomerPhone(rs.getString("customer_phone"));
                booking.setCustomerEmail(rs.getString("customer_email"));
                booking.setCheckInDate(rs.getDate("check_in_date"));
                booking.setCheckOutDate(rs.getDate("check_out_date"));
                booking.setTotalAmount(rs.getBigDecimal("total_amount"));
                booking.setStatus(rs.getString("status"));
                booking.setBookingDate(rs.getDate("booking_date"));
                
                rs.close();
                stmt.close();
                return booking;
            }
            
            rs.close();
            stmt.close();
            
        } catch (SQLException e) {
            System.err.println("获取预订信息失败: " + e.getMessage());
            e.printStackTrace();
        } finally {
            DBUtil.closeConnection(conn);
        }
        
        return null;
    }
}
